import pandas as pd
import os

# 读取数据
df = pd.read_csv('wuhan_price.csv')

# 确保数据类型正确
df['挂牌年份'] = df['挂牌年份'].astype(int)
df['单价平均价格'] = df['单价平均价格'].astype(float)
df['longitude'] = df['longitude'].astype(float)
df['latitude'] = df['latitude'].astype(float)

# 创建输出目录
output_dir = 'wuhan_price_by_year'
os.makedirs(output_dir, exist_ok=True)

# 按年份拆分数据并保存
for year in df['挂牌年份'].unique():
    df_year = df[df['挂牌年份'] == year][[
        'id', '区域', '商圈', '小区', '挂牌年份', '单价平均价格', 'longitude', 'latitude'
    ]]
    output_file = f'{output_dir}/wuhan_price_{year}.csv'
    df_year.to_csv(output_file, index=False, encoding='utf-8')
    print(f'Saved: {output_file}')